package org.elena.model.filter;

import org.apache.commons.lang3.StringUtils;
import org.elena.model.exception.ActivityException;
import org.elena.model.exception.NotSupportException;
import org.elena.model.result.MatchResult;
import org.elena.model.result.Result;
import org.elena.model.sample.Sample;

public class ChannelIdEqFilter extends ChannelFilter implements IdAttribute {

    private String channelId;

    @Override
    public String fetchSampleValue(Sample sample) {
        return sample.getChannelId();
    }

    @Override
    public Result doFilter(Sample sample) throws ActivityException {
        if (equals(sample)) {
            return new MatchResult(){{setSuccess(true);}};
        }
        return new MatchResult(){{setSuccess(false);}};
    }

    @Override
    public String getIdRange() {
        return channelId;
    }

    @Override
    public void addId(String id) {
        if (StringUtils.isBlank(channelId)) {
            channelId = id;
        }
    }

    @Override
    public boolean equals(Sample sample) throws NotSupportException {
        String sampleValue = fetchSampleValue(sample);
        if (StringUtils.isBlank(sampleValue)) {
            return false;
        }
        return getIdRange().equals(sampleValue);
    }
}
